package com.victor9ac.tfm.twitter.adapter;

import java.util.List;

import twitter4j.Status;

import com.victor9ac.mashup.api.Comment;
import com.victor9ac.mashup.api.CommentSource;
import com.victor9ac.mashup.api.Commentaries;
import com.victor9ac.mashup.port.CommentOperation;
import com.victor9ac.tfm.twitter.client.TwitterClient;

public class TwitterAdapter implements CommentOperation {

	TwitterClient twitterClient;
	
	public Commentaries getCommentariesFromKeyword(String keyword) {
		List<Status> tweets = twitterClient.getTweets(keyword);

		
		Commentaries commentaries = this.adapter(tweets);
		commentaries.setKeywordLink(keyword);
		
		return commentaries;

	}




	public void setTwitterClient(TwitterClient twitterClient) {
		this.twitterClient = twitterClient;
	}




	private Commentaries adapter(List<Status> status) {
		Commentaries commentaries = null;
		if (status != null && status.size() > 0) {
			commentaries = new Commentaries();
			for (Status currentStatus : status) {
				commentaries.add(this.adapter(currentStatus));
			}
		}
		return commentaries;
	}

	
	private Comment adapter(Status status) {
		Comment comment = null;
		if (status != null) {
			comment = new Comment(status.getUser().getName(), status.getText(),
					status.getCreatedAt(),CommentSource.TWITTER);
			
		}
		return comment;
	}

}
